package com.example.pm.ending.server.service.impl;

import com.example.pm.ending.server.ex.ServiceException;
import com.example.pm.ending.server.mapper.InvestorMapper;
import com.example.pm.ending.server.mapper.InvestorModuleMapper;
import com.example.pm.ending.server.mapper.PublishInvestorMapper;
import com.example.pm.ending.server.module.investor.AddNewInvestorModuleDTO;
import com.example.pm.ending.server.module.investor.InvestorModuleListVO;
import com.example.pm.ending.server.pojo.dto.InvestorUpdateDTO;
import com.example.pm.ending.server.pojo.entity.Admin;
import com.example.pm.ending.server.pojo.entity.Investor;
import com.example.pm.ending.server.pojo.vo.AdminStandardVO;
import com.example.pm.ending.server.pojo.vo.InvestorStandardVO;
import com.example.pm.ending.server.service.IInvestorModuleService;
import com.example.pm.ending.server.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class InvestorModuleModuleServiceImpl implements IInvestorModuleService {

    @Autowired
    private InvestorMapper investorMapper;
    @Autowired
    private InvestorModuleMapper investorModuleMapper;
    @Autowired
    private PublishInvestorMapper publishInvestorMapper;


    public InvestorModuleModuleServiceImpl() {
        log.debug("创建业务类对象：InvestorModuleModuleServiceImpl");
    }
    //添加业务
    @Override
    public void addNewInvestor(AddNewInvestorModuleDTO addNewInvestorModuleDTO) {
        log.debug("开始处理【添加投资商】的业务，参数：{}", addNewInvestorModuleDTO);
        String name = addNewInvestorModuleDTO.getInvestorName();
        int countByName = investorMapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, countByName);
        if (countByName>0){
            String message="添加投资商失败，投资商名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        String phone = addNewInvestorModuleDTO.getInvestorPhone();
        int countByPhone = investorMapper.countByPhone(phone);
        //判断手机号是否被使用
        Integer countPhone = investorMapper.countByPhone(addNewInvestorModuleDTO.getInvestorPhone());
        if (countPhone > 0) {
            String message = "该手机号已经被注册";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Investor investor = new Investor();
        investor.setName(addNewInvestorModuleDTO.getInvestorName());
        investor.setLogo(addNewInvestorModuleDTO.getInvestorLoge());
        investor.setPhone(addNewInvestorModuleDTO.getInvestorPhone());
        investor.setDescription(addNewInvestorModuleDTO.getInvestorDescription());
        investor.setEnable(ENABLE_STATE);
        log.debug("即将执行插入数据，参数：{}", investor);
        investorMapper.insert(investor);

    }
    //删除业务
    @Override
    public void deleteInvestor(Long id) {
        log.debug("开始处理【根据ID删除投资商】的业务，参数：{}", id);
        InvestorStandardVO investor = investorMapper.getInvestorById(id);
        log.debug("根据ID={}检查投资商数据是否存在，查询结果：{}", id, investor);
        if (investor == null){
            String message = "删除投资商失败，尝试删除的投资商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        log.debug("即将执行删除，参数：{}", id);
        log.debug("删除投资商成功,并清空该投资商相关的公告信息！");
        investorMapper.deleteById(id);



    }
    //修改业务
    @Override
    public void updateInvestorInfoById(InvestorUpdateDTO dto) {
        log.debug("开始处理【修改投资商详情】的业务，ID：{}，新数据：{}", dto.getId(), dto);
        InvestorStandardVO queryResult = investorMapper.getInvestorById(dto.getId());
        if (queryResult == null){
            String message = "修改投资商详情失败,尝试修改的投资商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int count = investorMapper.countByNameAndNotId(dto.getId(),dto.getName());
        if (count > 0){
            String message = "修改投资商详情失败，投资商名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Investor investor = new Investor();
        investor.setId(dto.getId());
        investor.setName(dto.getName());
        investor.setDescription(dto.getDescription());
        investor.setPhone(dto.getPhone());
        investor.setLogo(dto.getLogo());
        log.debug("即将执行修改后的投资商数据，参数：{}", investor);
        investorMapper.update(investor);
    }

    @Override
    public InvestorStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询投资商详情】的业务，参数：{}", id);
        // AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        InvestorStandardVO queryResult = investorMapper.getInvestorById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询投资商详情失败，投资商数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    //获取投资商列表信息
    @Override
    public List<InvestorModuleListVO> getInvestorList() {
        List<InvestorModuleListVO> investModuleList = investorModuleMapper.getInvestModuleList();
        return investModuleList;
    }

    // 启用投资商
    @Override
    public void investorEnable(Long investorId) {
        log.debug("开始处理【启用类别】的业务，参数：{}", investorId);
        updateEnableById(investorId,1);
    }
    // 禁用投资商
    @Override
    public void investorDisEnable(Long investorId) {
        log.debug("开始处理【禁用类别】的业务，参数：{}", investorId);
        updateEnableById(investorId, 0);
    }

    public void updateEnableById(Long id, Integer enable) {
        // 调用Mapper对象的getStandardById()方法执行查询
        InvestorStandardVO InvestorId = investorMapper.getInvestorById(id);
        // 判断查询结果是否为null，如果是，则抛出异常
        if (InvestorId == null) {
            String message = INVESTOR_ENABLE_TEXT[enable] + "类别失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable与参数enable是否相同，如果是，则抛出异常（当前状态与目标状态相同，没必要执行更新）
        if (InvestorId.getEnable() == enable) {
            String message = INVESTOR_ENABLE_TEXT[enable] + "类别失败，此类别已经处于" + INVESTOR_ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建Category对象
        Investor updateInvestor = new Investor();
        // 向Category对象中封装属性值：id, enable，均来自方法参数
        updateInvestor.setId(id);
        updateInvestor.setEnable(enable);
        // 调用Mapper对象的update()方法执行更新
        int rows = investorMapper.update(updateInvestor);
        if (rows != 1) {
            String message = INVESTOR_ENABLE_TEXT[enable] + "类别失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


}
